import { useCallback, useRef } from 'react';

export function useDebounce<T extends (...args: any[]) => any>(func: T, delay: number): T {
  const debounce = useCallback((...args: Parameters<T>) => {
    const handler = setTimeout(() => func(...args), delay);

    return () => {
      clearTimeout(handler);
    };
  }, [func, delay]) as T;

  return debounce;
}



export function useThrottle<T extends (...args: any[]) => any>(func: T, delay: number): T {
  const lastRun = useRef(Date.now());

  const throttledFunc = useCallback((...args: Parameters<T>) => {
    const now = Date.now();
    if (now - lastRun.current > delay) {
      func(...args);
      lastRun.current = now;
    }
  }, [func, delay]) as T;

  return throttledFunc;
}
